package com.xjtu.platform.mainAction;

import com.xjtu.platform.common.PageParam;
import com.xjtu.platform.common.Result;

import com.xjtu.platform.mainAction.dto.CreateDto;
import com.xjtu.platform.mainAction.dto.UpdateDto;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.rmi.server.RemoteServer;
import java.util.Map;


@RestController
@RequestMapping("/home")
public class Controller {

    @Resource
    private MainService mainService;

    @PostMapping("/111")
    public Result test(){
        return Result.success("1");
    }


    @PostMapping("/new_article")
    public Result create_article(@RequestBody CreateDto createDto){
        try {
            mainService.createArticle(createDto);
            return Result.success("创建成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,"系统错误");
        }
    }

    @PostMapping("/get_all_article")
    public Result getAllArticle(@RequestBody PageParam pageParam){
        try {
            Map<String,Object> map = mainService.getAllArticle(pageParam);
            return Result.success("成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,"系统错误");
        }
    }

    @PostMapping("/update_article")
    public Result updateArticle(@RequestBody UpdateDto updateDto){
        try {
            mainService.updateArticle(updateDto);
            return Result.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,"系统错误");
        }
    }




}
